package com.itheima.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {

    // 关键修复：在路径末尾添加斜杠 /
    private final String UPLOAD_ROOT = "D:/MONKEY/resource/CBSystem/";

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 保持原有的资源映射配置不变（现在路径会自动拼接正确）
        registry.addResourceHandler("/userPictures/**")
                .addResourceLocations("file:" + UPLOAD_ROOT + "userPictures/")
                .setCachePeriod(0);

        registry.addResourceHandler("/productPictures/**")
                .addResourceLocations("file:" + UPLOAD_ROOT + "productPictures/")
                .setCachePeriod(0);

        registry.addResourceHandler("/default-avatar.jpg")
                .addResourceLocations("file:" + UPLOAD_ROOT + "userPictures/default-avatar.jpg")
                .setCachePeriod(0);

        registry.addResourceHandler("/default-product.webp")
                .addResourceLocations("file:" + UPLOAD_ROOT + "productPictures/default-product.webp")
                .setCachePeriod(0);

        registry.addResourceHandler("/wechat.jpg")
                .addResourceLocations("file:" + UPLOAD_ROOT + "pay/wechat.jpg")
                .setCachePeriod(0);

        registry.addResourceHandler("/zfb.jpg")
                .addResourceLocations("file:" + UPLOAD_ROOT + "pay/zfb.jpg")
                .setCachePeriod(0);

        registry.addResourceHandler("/Market/**")
                .addResourceLocations("file:" + UPLOAD_ROOT + "Market/")
                .setCachePeriod(0);
    }
}
